﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace THO7AlgoritmTimerApplication {
    class brightness : VisionAlgorithm {
        float new_brightness = 0;
        public brightness(String name) : base(name) {}
        public override Bitmap DoAlgorithm(Bitmap sourceImage) {
            new_brightness = (float)getMarge(); 
            Bitmap dest = new Bitmap(sourceImage, sourceImage.Width, sourceImage.Height);
            BitmapData bmpdata = sourceImage.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            BitmapData bmpdata1 = dest.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
            IntPtr ptr = bmpdata.Scan0;
            IntPtr ptr1 = bmpdata1.Scan0;
            int bytes = Math.Abs(bmpdata.Stride) * sourceImage.Height;
            byte[] rgbValues = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            new_brightness = new_brightness / 100.0f;
            for (int counter = 0; counter < rgbValues.Length; counter += 3) {
                rgbValues[counter] = round_color(rgbValues[counter], new_brightness);
                rgbValues[counter + 1] = round_color(rgbValues[counter + 1], new_brightness);
                rgbValues[counter + 2] = round_color(rgbValues[counter + 2], new_brightness);
            }
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr1, bytes);
            sourceImage.UnlockBits(bmpdata);
            dest.UnlockBits(bmpdata1);
            return dest;
        }

        public byte round_color(byte value, float module) {
            byte val = value;
            val = (byte)(val * module);
            if (val > 255) {
                val = 255;
            } else if (val < 0) {
                val = 0;
            }
            return val;
        }
    }
}
